<?php
/**
 * @title 919. 完全二叉树插入器
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class CBTInserter {
    /**
     * @param TreeNode $root
     */
    function __construct($root) {
        $this->root = $root;
        $this->nodeArray = [$this->root];
        $this->i = 0;

        /**
         * @since 2024-05-08 BFS广度遍历
         */
        while($this->nodeArray){
            /**
             * @since 2024-05-08 下次遍历元素的集合
             */
            $this->nextNodeArray = [];

            /**
             * @since 2024-05-08 遍历当前元素集合
             */
            for($this->i=0; $this->i<count($this->nodeArray); $this->i++){
                $node = $this->nodeArray[$this->i];
                if($node->left === null){
                    break 2;
                } else {
                    $this->nextNodeArray[] = $node->left;
                }
                if($node->right === null){
                    break 2;
                } else {
                    $this->nextNodeArray[] = $node->right;
                }
            }

            /**
             * @since 2024-05-08 下次遍历
             */
            $this->nodeArray = $this->nextNodeArray;
        }
    }

    /**
     * @param Integer $val
     * @return Integer
     */
    function insert($val) {
        if($this->i == count($this->nodeArray)){
            $this->nodeArray = $this->nextNodeArray;
            $this->nextNodeArray = [];
            $this->i = 0;
        } else {}

        $node = $this->nodeArray[$this->i];
        if($node->left === null){
            $node->left = new TreeNode($val);
            $this->nextNodeArray[] = $node->left;
            return $node->val;
        } else {}
        if($node->right === null){
            $node->right = new TreeNode($val);
            $this->nextNodeArray[] = $node->right;
            $this->i++;
            return $node->val;
        }
    }

    /**
     * @return TreeNode
     */
    function get_root() {
        return $this->root;
    }
}

/**
 * Your CBTInserter object will be instantiated and called as such:
 * $obj = CBTInserter($root);
 * $ret_1 = $obj->insert($val);
 * $ret_2 = $obj->get_root();
 */


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.08_2.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
